<template>
    <div>
        <el-page-header @back="gotoback" title="返回">
            <template #content>
                <el-breadcrumb-item :to="{ path: '/organization/user' }">职务列表</el-breadcrumb-item>
            </template>
        </el-page-header>
    </div>
    <el-skeleton :rows="8" :loading="load" animated>
    <div class="header">
        <div class="head">
            <el-button type="primary" style="float: left;margin-top: 15px;margin-left: 10px;" @click="add">新增</el-button>
        </div>
        <div class="main">
            <el-table v-loading="loading" :data="tableData" border class="table" max-height="55vh">
                <el-table-column fixed prop="id" label="序号" type="index" width="80">
                    <template #default="score">
                        {{ (page - 1) * limit + score.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column fixed prop="name" label="职务名">
                </el-table-column>
                <el-table-column fixed prop="lv" label="等级">
                </el-table-column>
                <el-table-column fixed prop="isBan" label="禁用">
                    <template #default="scope">
                        <el-tag :type="scope.row.isBan == 0 ? 'success' : 'danger'">
                            {{ scope.row.isBan == 0 ? '否' : '是' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column fixed prop="useCount" label="使用">
                    <template #default="scope">
                    <el-tag>
                        {{ scope.row.useCount }}
                    </el-tag>
                </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button size="small" type="danger" @click="deletes(scope.row)" plain
                            v-if="scope.row.isBan == 0 && scope.row.useCount == 0">删除</el-button>
                        <el-button size="small" type="danger" v-else 
                            v-show="scope.row.isBan == 0 && scope.row.useCount != 0"
                            style="margin-left: -1px;" @click="del">禁用</el-button>
                        <el-button size="small" type="success" v-show="scope.row.isBan !=0"
                            plain @click="des">启用</el-button>
                        <el-button size="small" type="primary" @click="detail(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="example-pagination-block" style="margin-top: 1vh;">
            <el-pagination @current-change="changes" :page-size="limit" :current-page="page" layout="prev, pager, next"
                :total="total" />
        </div>
    </div>
</el-skeleton>
</template>
<script lang="ts">
import { defineComponent, reactive, onMounted, toRefs } from 'vue'
import { GetZhiwuList,DeleteZhiwu } from '../../../api/user/user'
import { ElMessageBox } from 'element-plus';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';

export default defineComponent({
    setup() {
        const router = useRouter();
        const data = reactive({
            loading: true,
            tableData: [],
            limit: 10,//每页显示条数
            page: 1,//当前页
            total: 0,//总条数
            load:true,//骨架屏
        })
        const events = {
            data() {
                GetZhiwuList({
                    page: data.page,
                    limit: data.limit,
                }).then((res: any) => {
                    console.log('职务列表---', res)
                    data.tableData = res.data.data
                    data.loading = false
                    data.load = false
                    data.total = res.data.total
                })
            },
            //返回上一级
            gotoback() {
                window.history.go(-1)
            },
            //删除
            deletes(obj:any) {
                ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    data.loading = true
                    DeleteZhiwu({
                        ids: [obj.id]
                    }).then((res:any)=>{
                        if (res.result === 0) {
                            ElMessage({
                                message: '删除成功',
                                type: 'success',
                            })
                            events.data()
                        } else {
                            ElMessage.error({
                                message: '删除失败',
                                type: 'error',
                            })
                        }
                        data.loading = false
                    })
                }).catch(() => {
                    ElMessage.info("已取消删除")
                })

            },
            //跳转新增
            add(){
                router.push("/organization/zhiwu/add")
            },
            //禁用
            del(){

            },
            //启用
            des(){

            },
            //跳转详情页面
            detail(val:any) {
                console.log("详情id", val.id)
                router.push({path:`/organization/zhiwu/detail/${val.id}`})
            },
            //点击page触发事件
            changes(val: any) {
                data.loading = true
                data.page = val
                events.data()
            },
        }
        onMounted(() => {
            events.data()
        })
        return {
            ...toRefs(data),
            ...events,
        }
    },
})
</script>
<style scoped>
.head {
    border: #ada5a5 solid 2px;
    height: 60px;
    box-shadow: 3px 1px 1px 1px #ccc;
    border-radius: 5px;
    margin-top: 3vh;
    width: 96%;
    margin-left: 3vh;
}

.header {
    border: #ccc solid 1px;
    margin-top: 2vh;
    background-color: rgba(239, 240, 245, 0.932);
    border-radius: 5px;
}

.main {
    margin-top: 3vh;
    box-shadow: 3px 1px 1px 1px #ccc;
    margin-left: 3vh;
}

.table {
    width: 98%;
    border-radius: 10px;
    border: #ada5a5 solid 2px;
}
</style>
